<?php

/* 
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

function X_param(&$params,$key,$default = null,$dont_trim = false) {

  /**

  Return the value of a parameter corresponding to a given key, or a default
  value if the key is not present.

  \note
  The parameter and its value, if any, is removed by this function. This helps
  tracking down unused parameters -- see invoke_api() for more information.

  \param $params Associative array made of key-value pairs.
  \param $key Parameter (key).
  \param $default Default value (default = \c null).

  \return Either the user-supplied value, or the developer-supplied (default)
          value.

  \see params_get_array(), params_get_assoc(), params_get_localized().

  **/

  $pval = isset($params[$key]) ? "{$params[$key]}" : $default;

  unset($params[$key]); // remove key

  return $dont_trim || $pval === null ? $pval : trim($pval);

}

function X_param_array(&$params,$key,$default = null) {

  /**

  Similar to params_get(), but returns an array by exploding a comma-separated
  list of values.

  \warning This function is unsuitable for generic comma-separated input,
  where individual items could contain commas.

  \note
  The parameter and its value, if any, is removed by this function. This helps
  tracking down unused parameters -- see invoke_api() for more information.

  \param $params Associative array made of key-value pairs.
  \param $key Parameter (key).
  \param $default Default value; may be an array or a comma-separated list of
         strings (default = \c null).

  \return Either the user-supplied value, or the developer-supplied (default)
          value.

  \see params_get(), params_get_assoc(), params_get_localized().

  **/

  if (isset($params[$key])) {

    // do not remove the double quotes below; they make sure that the value
    // being exploded is a string

    $value = is_array($params[$key]) ? $params[$key] : "{$params[$key]}";
    $value = ($value === '') ? array() : is_array($params[$key]) ? $value : explode(',',$value);

    unset($params[$key]); // remove key

    return $value;

  }

  unset($params[$key]); // remove key

  return is_string($default) ? explode(',',$default) : $default;

}


/*
 * Sanitize output 
 */
function out($param) {
    return $param;
}

/**

  Return a random token string (cookie-like); not guaranteed to be unique, but
  close enough for our purposes -- assuming an even distribution of random
  numbers, this algorithm generates a duplicate once every 62^64 iterations.

  **/


function mk_random_token($length = 4) {

    $characters = '123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNPQRSTUVWXYZ';
    $random_string = '';
    for ($i = 0; $i < $length; $i++) {
        $random_string .= $characters[rand(0, strlen($characters) - 1)];
    }
    return $random_string;
}

//Adds custom query to field
function X_field(&$param,$query,$alias = null) {
    return $param[-1][$alias] = $query;
}
